<?php

/*
 * core/conf.php
 *
 * provide functions to access config
 */

// TODO: find a way to check if config name=>value exist

class Conf
{
	/*
	 * reference to configuration array
	 * use this to read it, be carefull if writing in it use resetSession() if necessary!
	 */
	public static $c = null;

	/*
	 * void init()
	 * initialize configuration system
	 */
	public static function init()
	{
		if( !Conf::isSessInitialized() )
		{
			Conf::resetSession( true );
		}

		Conf::load();

		// keep a reference to simplify writing
		Conf::$c =& $_SESSION['core']['conf']['config'];
	}

	/*
	 * resetSession()
	 * reinitialize session
	 */
	public static function resetSession( $resetCurrentConfig = false )
	{
		$_SESSION['core']['conf'] = array();
		$_SESSION['core']['conf']['core_ctime'] = 0;
		$_SESSION['core']['conf']['site_ctime'] = 0;
		if( $resetCurrentConfig )
			$_SESSION['core']['conf']['config'] = array();
	}

	/*
	 * isSessInitialized()
	 * return if configuration is already loaded
	 */
	public static function isSessInitialized()
	{
		if( !isset($_SESSION['core']['conf']) || is_null($_SESSION['core']['conf']) )
		{
			return false;
		}
		return true;
	}

	/*
	 * array load()
	 * load configuration files (core/config.php and site/config.php) and store them in a session array.
	 * site/config.php options overwrite core/config.php options.
	 */
	public static function load()
	{
		// config files filenames (depending from where we are called)
		$coreFilenames = array( 'core/config.php', '../core/config.php', 'backend/core/config.php' );
		$siteFilenames = array( 'site/config.php', '../site/config.php', 'backend/site/config.php' );

		// find which config files exist first
		$coreFile = Conf::firstFound( $coreFilenames );
		if( is_null($coreFile) )
		{
			die( 'Conf::load: unable to find core configuration file' );
		}

		$siteFile = Conf::firstFound( $siteFilenames );
		if( is_null($siteFile) )
		{
			die( 'Conf::load: unable to find site configuration file' );
		}

		$coreConfig = array();
		$siteConfig = array();

		// load config files
		if( filectime($coreFile) > $_SESSION['core']['conf']['core_ctime'] )
		{
			// core/config.php has changed, reload all
			$coreConfig = Conf::loadConfig( $coreFile );
			$siteConfig = Conf::loadConfig( $siteFile );
			$_SESSION['core']['conf']['core_ctime'] = filectime( $coreFile );
			$_SESSION['core']['conf']['site_ctime'] = filectime( $siteFile );
		}
		else
		{
			// core/config.php didn't changed, check if site/config.php has changed
			if( filectime($siteFile) > $_SESSION['core']['conf']['site_ctime'] )
			{
				$siteConfig = Conf::loadConfig( $siteFile );
				$_SESSION['core']['conf']['site_ctime'] = filectime( $siteFile );
			}
		}

		// create a merged version of all configuration options/values
		$_SESSION['core']['conf']['config'] = array_merge( $_SESSION['core']['conf']['config'], $coreConfig, $siteConfig );
	}

	/*
	 * filename firstFound( $filenames )
	 * return the first given filenames which exist
	 */
	public static function firstFound( $filenames )
	{
		foreach( $filenames as $file )
		{
			if( file_exists($file) )
				return $file;
		}
		return null;
	}

	/*
	 * array loadConfig( filenames )
	 * filenames: array of filenames to try to load
	 * return: array($filename_founded, $array_options_values)
	 */
	public function loadConfig( $filename )
	{
		// define $config, in case file doesn't
		$config = array();
		include( $filename );

		if( is_null($config) )
		{
			die( 'Conf::loadConfig: configuration file ('.$file.') does not fill an array called $config' );
		}

		return $config;
	}

};

Conf::init();

?>
